package com.shycloud.mido.member.service.impl;

import java.time.LocalDateTime;

import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.shycloud.mido.common.core.constant.CommonConstants;
import com.shycloud.mido.common.core.exception.BusinessException;
import com.shycloud.mido.common.core.util.R;
import com.shycloud.mido.member.api.entity.MemberRole;
import com.shycloud.mido.member.api.entity.SmMemberScanCode;
import com.shycloud.mido.member.api.entity.SmMemberScanInfo;
import com.shycloud.mido.member.api.util.Constant;
import com.shycloud.mido.member.api.vo.SmMemberScanCodeVO;
import com.shycloud.mido.member.mapper.MemberRoleMapper;
import com.shycloud.mido.member.mapper.SmMemberScanCodeMapper;
import com.shycloud.mido.member.mapper.SmMemberScanInfoMapper;
import com.shycloud.mido.member.service.SmMemberScanCodeService;

import lombok.AllArgsConstructor;

@Service
@AllArgsConstructor
public class SmMemberScanCodeServiceImpl extends ServiceImpl<SmMemberScanCodeMapper, SmMemberScanCode> implements SmMemberScanCodeService {

	/** 扫码业务 mapper */
	private SmMemberScanCodeMapper scanCodeMapper;
	/** 会员业务 mapper */
	private MemberRoleMapper memberRoleMapper;
	/** 用户扫描详情 mapper */
	private SmMemberScanInfoMapper scanInfoMapper;

	@Override
	@Transactional(rollbackFor = Exception.class, timeout = 36000)
	public SmMemberScanCodeVO scan(String keyword, String memberId) {

		//判断扫码次数
		SmMemberScanCode smMemberScanCode = scanCodeMapper.selectOne(new QueryWrapper<SmMemberScanCode>().eq(SmMemberScanCode.COL_KEY_WORD, keyword));

		if (smMemberScanCode == null) {
			throw new BusinessException(new R(CommonConstants.FAIL, "二维码已失效，请联系管理员~", 0));
		}

		if (smMemberScanCode.getDuration() == 0) {
			return new SmMemberScanCodeVO(0);
		}

		SmMemberScanCodeVO smMemberScanCodeVO = new SmMemberScanCodeVO();

		BeanUtils.copyProperties(smMemberScanCode, smMemberScanCodeVO);

		// 整数年
		if (smMemberScanCodeVO.getDuration() % 360 == 0) {
			smMemberScanCodeVO.setPlaceholder((smMemberScanCodeVO.getDuration() / 360) + "年");
		}

		// 整数月
		if (smMemberScanCodeVO.getDuration() < 360 && smMemberScanCodeVO.getDuration() % 30 == 0) {
			smMemberScanCodeVO.setPlaceholder((smMemberScanCodeVO.getDuration() / 30) + "个月");
		}

		// 年月分开
		if (smMemberScanCodeVO.getDuration() > 360 && smMemberScanCodeVO.getDuration() % 360 != 0) {

			//年
			Integer year = smMemberScanCodeVO.getDuration() / 360;
			//月
			Integer month = (smMemberScanCodeVO.getDuration() % 360) / 30;
			//日
			Integer day = (smMemberScanCodeVO.getDuration() % 360) % 30;

			smMemberScanCodeVO.setPlaceholder(year + "年" + month + "个月" + (day != 0 ? (day + "天") : ""));

		}

		SmMemberScanInfo smMemberScanInfo = scanInfoMapper.selectOne(new QueryWrapper<SmMemberScanInfo>().eq(SmMemberScanInfo.COL_MEMBER_ID, memberId));

		if (smMemberScanInfo != null) {
			throw new BusinessException(new R(CommonConstants.FAIL, "您已扫码领过奖了", smMemberScanCodeVO));
		}

		//二维码已失效
		if (smMemberScanCode.getCountCan() <= 0) {
			throw new BusinessException(new R(CommonConstants.FAIL, "二维码已失效", 2));
		}

		//会员充值
		//续费 or 新增会员
		//充值会员
		MemberRole role = memberRoleMapper.selectOne(new QueryWrapper<MemberRole>()
				.eq("member_id", memberId)
				.and(x -> x.eq("role_code", Constant.VIP_MEMBER)));

		if (role != null) {

			//创建时间
			role.setCreateTime(LocalDateTime.now());

			//如果还没到期，那么从到期时间开始累加
			if (role.getEndTime().isAfter(LocalDateTime.now())) {
				role.setEndTime(role.getEndTime().plusDays(smMemberScanCode.getDuration()));
			}
			//如果已经到期，那么从今天开始累加会员天数
			else {
				role.setStartTime(LocalDateTime.now());
				role.setEndTime(LocalDateTime.now().plusDays(smMemberScanCode.getDuration()));
			}
			memberRoleMapper.updMemberRole(role);
		} else {
			//新增会员
			role = new MemberRole();
			//创建时间
			role.setCreateTime(LocalDateTime.now());
			//开始时间
			role.setStartTime(LocalDateTime.now());
			//结束时间
			role.setEndTime(LocalDateTime.now().plusDays(smMemberScanCode.getDuration()));
			//会员
			role.setRoleCode(Constant.VIP_MEMBER);
			//用户ID
			role.setMemberId(memberId);
			memberRoleMapper.saveMemberRole(role);
		}

		//累加扫码次数
		smMemberScanCode.setCountCan(smMemberScanCode.getCountCan() - 1);
		scanCodeMapper.updateById(smMemberScanCode);

		//标识此用户码已经扫过了
		SmMemberScanInfo smMemberScanInfoNew = new SmMemberScanInfo();
		smMemberScanInfoNew.setFromMember(smMemberScanCode.getComment());
		smMemberScanInfoNew.setMemberId(memberId);
		scanInfoMapper.insert(smMemberScanInfoNew);

		return smMemberScanCodeVO;
	}

}
